/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "sha1.h"
#include "bignumber.h"
#include <cstdarg>

namespace node {
    namespace crypto {

        Sha1Hash::Sha1Hash(void)
        {
            SHA1_Init(&ctx_);
        }

        Sha1Hash::~Sha1Hash(void)
        {
            SHA1_Init(&ctx_);
        }

        void Sha1Hash::initialize(void)
        {
            SHA1_Init(&ctx_);
        }

        void Sha1Hash::finalize(void)
        {
            SHA1_Final(digest_, &ctx_);
        }

        void Sha1Hash::updateData(const ::std::uint8_t* dat, ::std::size_t len)
        {
            SHA1_Update(&ctx_, (const void*)dat, len);
        }

        void Sha1Hash::updateData(const ::std::string& str)
        {
            updateData((const ::std::uint8_t*)str.c_str(), str.length());
        }

        void Sha1Hash::updateBigNumbers(BigNumber* x, ...)
        {
            std::va_list v;
            BigNumber* bn;

            va_start(v, x);
            bn = x;
            while (bn)
            {
                updateData(bn->asByteArray(), bn->getNumBytes());
                bn = va_arg(v, BigNumber*);
            }
            va_end(v);
        }

    }
}